$(function () {
    // 获取文章分类
    $.ajax({
        url: BigNew.category_list,
        type: 'get',
        success: function (backData) {

            if (backData.code == 200) {
                let resHtml = template('slugTemp', backData);
                $('#selCategory').html(resHtml);

            };
        },
    });
    //文章请求
    function getList(page, callback) {
        let type = $('#selCategory').val();
        let state = $('#selStatus').val();
        $.ajax({
            url: BigNew.article_query,
            type: 'get',
            data: {
                type,
                state,
                page,
                perpage: 10
            },
            success(backData) {
                // console.log(backData);
                if (backData.code == 200) {
                    let resHtml = template('textTemp', backData.data);
                    $('tbody').html(resHtml);

                    if (backData.data.totalCount == 0) {
                        $('.hint').show();
                        $('#page').hide();
                        return
                    } else {
                        $('.hint').hide();
                        $('#page').show();
                        if (callback) {
                            callback(backData);

                        } else {

                            //文章分页
                            let $twbs = $('#page').twbsPagination({
                                totalPages: backData.data.totalPage,
                                visiblePages: 7,
                                first: '首页',
                                prev: '上一页',
                                next: '下一页',
                                last: '尾页',
                                onPageClick: function (event, page) {
                                    getList(page);
                                }

                            });


                        };

                    }


                };
            },
        });
    }
    //获取文章
    getList(1);

    // 筛选文章
    $('#btnSearch').on('click', function (e) {
        e.preventDefault()
        getList(1, function (backData) {
            let totalPage = backData.data.totalPage;
            let localTotalpage = $('#page').twbsPagination('getTotalPages');
            if (totalPage != localTotalpage || totalPage == localTotalpage) {
                $('#page').twbsPagination('changeTotalPages', totalPage, 1)
            }



        });
    });

    // 删除文章
    $('tbody').on('click', '.btn-delete', function () {
        if (confirm('是否确认删除文章？')) {
            let id = $(this).parent().attr('data-id');
            let _this = $(this)
            $.ajax({
                url: BigNew.article_delete,
                type: 'post',
                data: {
                    id
                },
                success(backData) {
                    if (backData.code == 204) {
                        alert('删除成功');
                        _this.parent().parent().remove();

                        let currentPage = $('#page').twbsPagination('getCurrentPage');
                        getList(currentPage, function (backData) {
                            let totalPage = backData.data.totalPage;
                            let localTotalpage = $('#page').twbsPagination('getTotalPages');

                            if (totalPage != localTotalpage) {
                                if (currentPage > totalPage) {
                                    currentPage = totalPage;
                                }

                                $('#page').twbsPagination('changeTotalPages', totalPage, currentPage);
                            };


                        });
                    };
                },
            });
        };
    });
});